#coding=utf-8
import xlrd
import logging
import sys
from sheetReader import *
import importlib

importlib.reload(sys)

SheetName_Output = "Output"
Col_SheetName = "页名"
Col_Path = "路径"
Col_Name = "名字"

class ExcelReader:
	def __init__(self, filePath):
		self._filePath = filePath
		try :
			self._workbook = xlrd.open_workbook(self._filePath)
		except BaseException as e :
			logging.error("open xls file(%s) failed!" %(self._filePath))
			raise

	def ReadOutputSheets(self):
		try :
			self._sheetOutput = self._workbook.sheet_by_name(SheetName_Output)
		except Exception as e:
			logging.error("open sheet(%s) failed!" %(SheetName_Output))

		self._row_count = self._sheetOutput.nrows
		self._col_count = self._sheetOutput.ncols

		mapColName2index = {}
		for colNum in range(0, self._col_count):
			colName = str(self._sheetOutput.cell(0, colNum).value).strip()
			mapColName2index[colName] = colNum

		listSheet = []
		for rowNum in range(1, self._row_count):
			sheetName = str(self._sheetOutput.cell(rowNum, mapColName2index[Col_SheetName]).value).strip()
			path = str(self._sheetOutput.cell(rowNum, mapColName2index[Col_Path]).value).strip()
			name = str(self._sheetOutput.cell(rowNum, mapColName2index[Col_Name]).value).strip()
			listSheet.append(SheetReader(self._workbook.sheet_by_name(sheetName), path, name))

		return listSheet